<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button
        class="check-button"
        :is-check="isAllSelect"
        @click.native="checkClick"
      />

      <span>全选</span>
    </div>
    <div class="price">合计：{{ totalPrice }}</div>
    <div class="calculate" @click="calcClick">去计算({{ checkLength }})</div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton.vue";
import { mapGetters } from "vuex";
export default {
  components: { CheckButton },
  computed: {
    ...mapGetters(["cartList"]),
    totalPrice() {
      return (
        "￥" +
        this.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    checkLength() {
      return this.cartList.filter((item) => {
        return item.checked;
      }).length;
    },
    isAllSelect() {
      //方法1
      // if (this.checkLength == 0) {
      //   return false;
      // }
      // return this.checkLength == this.cartList.length;
      //方法2 需要遍历所有
      // return !this.cartList.filter((item) => {
      //   return !item.checked;
      // }).length;
      //方法3
      // if (this.cartList.length  == 0) {
      //   return false;
      // }
      return (
        !!this.cartList.length && !this.cartList.find((item) => !item.checked)
      );
    },
  },
  methods: {
    checkClick() {
      //全部选中
      let check = this.isAllSelect;
      if (check) {
        this.cartList.forEach((item) => {
          item.checked = !check;
        });
      } else {
        this.cartList.forEach((item) => {
          item.checked = !check;
        });
      }
    },
    calcClick() {
      if (!this.checkLength) {
        this.$toast.show("请选择购买的商品", 2000);
      }
    },
  },
};
</script>

<style>
.bottom-bar {
  position: relative;
  display: flex;
  height: 40px;
  line-height: 40px;

  background-color: #eee;
  font-size: 14px;
}
.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
  /* vertical-align: middle;   */
}
.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
  margin-right: 5px;
}
.price {
  margin-left: 20px;
  flex: 1;
}
.calculate {
  width: 90px;
  background-color: rgba(255, 0, 0, 0.8);
  text-align: center;
  color: #fff;
}
</style>